package ac.snu.street;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;


public class MyPostActivity extends ListActivity {
	private List<StreetNote> myNotes;
	private List<String> titles;
	private ArrayAdapter<String> adapter;
	private Context mContext;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_post);

		Bundle b = getIntent().getExtras(); 
		myNotes = b.getParcelableArrayList(StreetPostActivity.KEY_MYPOST);
		
		titles = new ArrayList<String>();
		for(StreetNote note : myNotes) {
			titles.add(note.title);
		}
		
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, titles);
		setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		final int pos = position;
		final StreetNote note = myNotes.get(position);
		final LinearLayout linear = (LinearLayout) View.inflate(MyPostActivity.this,R.layout.reply, null);
		mContext = MyPostActivity.this;
		
		AlertDialog.Builder dialog  = new AlertDialog.Builder(this); 
		dialog.setTitle(note.title);
		dialog.setMessage(note.contents);
		dialog.setIcon(R.drawable.postit);
		
		dialog.setPositiveButton("답변달기", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
				AlertDialog.Builder dialogReply = new AlertDialog.Builder(mContext);
				
	    	      dialogReply.setTitle("답변달기");
	    	      dialogReply.setView(linear);
	    	 	  dialogReply.setPositiveButton("답변", new DialogInterface.OnClickListener() {
	    	    	  @Override
	    	    	  public void onClick(DialogInterface dialog, int which) {
	    	    		 
	    				// TODO Auto-generated method stub
	    	    		  
	    	    		    EditText txt = (EditText)linear.findViewById(R.id.replyTxt);
	    					String txtContent = txt.getText().toString();
	    					
	    					StreetPostActivity.remoteDB.update(note.id, note.contents + "\n  Re : " + txtContent);
	    					
	    	    	  }
	    	 	  });
	    	 	  
	    	 	 
	    	 	 dialogReply.setNegativeButton("취소", new DialogInterface.OnClickListener() {
	    	 		@Override
	    			public void onClick(DialogInterface dialog, int which) {
	    				// Auto-generated method stub
	    			}
	    	 	 });
	    	 	 dialogReply.show();
			}
		});
		
		dialog.setNeutralButton("삭제하기", new DialogInterface.OnClickListener() {
	    	  @Override
	    	  public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
	    		    
	    		  	// remove from server
					StreetPostActivity.remoteDB.delete(note.id);
					
					// remove from current list activity
					myNotes.remove(pos);
					titles.remove(pos);
					adapter.notifyDataSetChanged();
	    	  }
	 	  });
		
		
		dialog.setNegativeButton("취소", new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// Auto-generated method stub
			}
		});
		dialog.show();
		
		
	}
}
